﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DA
{
    public class NhanVien_DA
    {
        QLKhachSanDataContext ql;
        public List<NhanVien> TimKiemNVTheoMA(string manv)
        {
            try
            {
                ql = new QLKhachSanDataContext();
                return ql.NhanViens.Where(p => p.MaNV.Contains(manv)).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ql.Dispose();
            }
        }
        public List<NhanVien> TimKiemNVTheoTEN(string tennv)
        {
            try
            {
                ql = new QLKhachSanDataContext();
                return ql.NhanViens.Where(p => p.TenNV.Contains(tennv)).ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ql.Dispose();
            }
        }
        public List<NhanVien> LayDSNhanVien()
        {
            try
            {
                ql = new QLKhachSanDataContext();
                return ql.NhanViens.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ql.Dispose();
            }
        }
        public bool ThemNhanVien(NhanVien n)
        {
            try
            {
                ql = new QLKhachSanDataContext();
                NhanVien nv = new NhanVien();
                nv.MaNV = n.MaNV;
                nv.Password = n.Password;
                nv.TenNV = n.TenNV;
                nv.GioiTinh = n.GioiTinh;
                nv.NgaySinh = n.NgaySinh;
                nv.DiaChi = n.DiaChi;
                nv.NgayVaoLam = n.NgayVaoLam;
                nv.MaCV = n.MaCV;
                nv.CMND = n.CMND;
                nv.Avatar = n.Avatar;
                ql.NhanViens.InsertOnSubmit(nv);
                ql.SubmitChanges();
                return true;
            }
            catch(Exception ex)
            {
                ex.Message.ToString();
                return false;
            }
            finally
            {
                ql.Dispose();
            }
        }
        public bool SuaNhanVien(NhanVien n)
        {
            try
            {
                ql = new QLKhachSanDataContext();
                NhanVien nv = ql.NhanViens.Where(p=>p.MaNV==n.MaNV).FirstOrDefault();
                nv.Password = n.Password;
                nv.TenNV = n.TenNV;
                nv.GioiTinh = n.GioiTinh;
                nv.NgaySinh = n.NgaySinh;
                nv.DiaChi = n.DiaChi;
                nv.NgayVaoLam = n.NgayVaoLam;
                nv.MaCV = n.MaCV;
                nv.CMND = n.CMND;
                nv.Avatar = n.Avatar;
                ql.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                ql.Dispose();
            }
        }
        public bool XoaNhanVien(string manv)
        {
            try
            {
                ql = new QLKhachSanDataContext();
                NhanVien nv = ql.NhanViens.Where(p => p.MaNV == manv).FirstOrDefault();
                ql.NhanViens.DeleteOnSubmit(nv);
                ql.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                ql.Dispose();
            }
        }
        public string LayPassTheoManv(string manv)
        {
            try
            {
                ql = new QLKhachSanDataContext();
                return ql.NhanViens.Where(p => p.MaNV == manv).Select(p=>p.Password).FirstOrDefault();
            }
            catch
            {
                return null;
            }
            finally
            {
                ql.Dispose();
            }
        }
    }
}
